package com.qzdsoft.erpcloud.service.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qzdsoft.erpcloud.domain.sys.SysRole;
import com.qzdsoft.erpcloud.domain.sys.SysRoleTable;
import com.qzdsoft.erpcloud.domain.sys.SysRoleTableField;
import com.qzdsoft.erpcloud.mapper.sys.SysRoleMapper;
import com.qzdsoft.erpcloud.mapper.sys.SysRoleTableFieldMapper;
import com.qzdsoft.erpcloud.mapper.sys.SysRoleTableMapper;
import com.qzdsoft.erpcloud.service.sys.SysRoleTableService;
import com.qzdsoft.erpcloud.vo.sys.SysRoleTableEditInfo;
import com.qzdsoft.erpcloud.vo.sys.SysRoleTableQueryParam;
import com.qzdsoft.erpcloud.vo.sys.ZtreeNode;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.sys.SysRoleStatus;

/**
 * Created by Admin
 */
@Service
public class SysRoleTableServiceImpl implements SysRoleTableService {
	private static final Logger logger = LoggerFactory.getLogger(SysRoleTableServiceImpl.class);
	
    @Autowired
    private SysRoleTableMapper sysRoleTableMapper;
    @Autowired
    private  SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleTableFieldMapper sysRoleTableFieldMapper;
    
    @Override
    public SysRoleTable selectById(Integer id) {
        return sysRoleTableMapper.selectByPrimaryKey(id);
    }

    @Override
    public Response saveOrUpd(SysRoleTable sysRoleTable) {
//        if(null != sysRoleTable.getId()){
//            sysRoleTable.setRole_id(null);
//            sysRoleTable.setTable_id(null);
//            sysRoleTableMapper.updateByPrimaryKey(sysRoleTable);
//        } else {
//            if(null == sysRoleTable.getRole_id() || null == sysRoleTable.getTable_id()){
//                return Response.error("角色或表格权限不得为空!");
//            }
//
//            Integer count = selectDataCount(sysRoleTable.getRole_id(), sysRoleTable.getTable_id());
//            if(null != count && count.intValue() > 0){
//                return Response.error("已经存在!");
//            }
//            sysRoleTableMapper.insert(sysRoleTable);
//        }
        return Response.success(null);
    }

    @Override
    public Response del(Integer id) {
    	 SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
         sysRole.setStatus(Integer.parseInt(SysRoleStatus.DEL_CODE));
         sysRoleMapper.updateByPrimaryKey(sysRole);;
        return Response.success(null);
    }


    @Override
    public List<SysRoleTable> selectData(SysRoleTableQueryParam info) {
        return sysRoleTableMapper.selectData(info);
    }

	@Override
	public Response<ZtreeNode> findAllTable() {
		List<ZtreeNode> list = sysRoleTableMapper.findAllTable();
		return new Response(ResultEnum.SUCCESS,list);
	}

	@Override
	@Transactional
	public Response saveRoleTableField(SysRoleTableEditInfo info) {
//		int fcount = sysRoleTableFieldMapper.findByRoleId(info.getRoleId());
		if(info.getIds().size()==0) {//清除表格权限
			int rc = sysRoleTableFieldMapper.removeField(info.getRoleId(),info.getTableId());
		}else{
			int rc = sysRoleTableFieldMapper.removeField(info.getRoleId(),info.getTableId());
			int count = 0;
			for(int i = 0;i<info.getIds().size();i++) {
				SysRoleTableField fd = new SysRoleTableField();
				fd.setFieldId(info.getIds().get(i).toString());
				fd.setRoleId(info.getRoleId());
				count += sysRoleTableFieldMapper.insert(fd);
//				feilds.add(fd);
			}
			logger.debug("sieze={},count={}",info.getIds().size(),count);
//			int incount = sysRoleTableFieldMapper.insertList(feilds);
			if(count != info.getIds().size()){
				logger.error("角色表格字段权限设置保存错误:保存记录数={},实际记录数={}",count,info.getIds().size());
				throw new RuntimeException("表格权限设置失败，保存记录数与实际记录数不符！");
			}
		}
		return new Response(ResultEnum.SUCCESS);
	}
    
    
}
